﻿using Microsoft.AspNetCore.Identity;

namespace Devonline.Identity;

public static class IdentityErrorDescriberExtensions
{
    /// <summary>
    /// Returns the default <see cref="IdentityError"/>.
    /// </summary>
    /// <returns>The default <see cref="IdentityError"/>.</returns>
    public static IdentityError ChangeFailure(this IdentityErrorDescriber errorDescriber, string type, string value)
    {
        return new IdentityError
        {
            Code = nameof(ChangeFailure),
            Description = $"修改 {type} 为 {value} 失败!"
        };
    }

    /// <summary>
    /// Returns an <see cref="IdentityError"/> indicating the specified <paramref name="group"/> name is invalid.
    /// </summary>
    /// <param name="group">The invalid role.</param>
    /// <returns>An <see cref="IdentityError"/> indicating the specific group <paramref name="group"/> name is invalid.</returns>
    public static IdentityError InvalidGroupName(this IdentityErrorDescriber errorDescriber, string group)
    {
        return new IdentityError
        {
            Code = nameof(InvalidGroupName),
            Description = $"组织 {group} 不存在!"
        };
    }

    /// <summary>
    /// Returns an <see cref="IdentityError"/> indicating a user is already in the specified <paramref name="group"/>.
    /// </summary>
    /// <param name="user">The duplicate user.</param>
    /// <param name="group">The duplicate group.</param>
    /// <returns>An <see cref="IdentityError"/> indicating a user is already in the specified <paramref name="group"/>.</returns>
    public static IdentityError UserAlreadyInGroup(this IdentityErrorDescriber errorDescriber, string user, string group)
    {
        return new IdentityError
        {
            Code = nameof(UserAlreadyInGroup),
            Description = $"用户 {user} 已在组织 {group} 中"
        };
    }

    /// <summary>
    /// Returns an <see cref="IdentityError"/> indicating a user is not in the specified <paramref name="group"/>.
    /// </summary>
    /// <param name="user">The duplicate user.</param>
    /// <param name="group">The duplicate group.</param>
    /// <returns>An <see cref="IdentityError"/> indicating a user is not in the specified <paramref name="group"/>.</returns>
    public static IdentityError UserNotInGroup(this IdentityErrorDescriber errorDescriber, string user, string group)
    {
        return new IdentityError
        {
            Code = nameof(UserNotInGroup),
            Description = $"用户 {user} 不在组织 {group} 中"
        };
    }

    /// <summary>
    /// Returns an <see cref="IdentityError"/> indicating a user is already in the specified <paramref name="resource"/>.
    /// </summary>
    /// <param name="user">The duplicate user.</param>
    /// <param name="resource">The duplicate resource.</param>
    /// <returns>An <see cref="IdentityError"/> indicating a user is already in the specified <paramref name="resource"/>.</returns>
    public static IdentityError UserAlreadyInAccessRule(this IdentityErrorDescriber errorDescriber, string user, string resource)
    {
        return new IdentityError
        {
            Code = nameof(UserAlreadyInAccessRule),
            Description = $"用户 {user} 对资源 {resource} 的访问规则已存在"
        };
    }

    /// <summary>
    /// Returns an <see cref="IdentityError"/> indicating a role is already in the specified <paramref name="resource"/>.
    /// </summary>
    /// <param name="role">The duplicate role.</param>
    /// <param name="resource">The duplicate resource.</param>
    /// <returns>An <see cref="IdentityError"/> indicating a role is already in the specified <paramref name="resource"/>.</returns>
    public static IdentityError RoleAlreadyInAccessRule(this IdentityErrorDescriber errorDescriber, string role, string resource)
    {
        return new IdentityError
        {
            Code = nameof(RoleAlreadyInAccessRule),
            Description = $"角色 {role} 对资源 {resource} 的访问规则已存在"
        };
    }

    /// <summary>
    /// Returns an <see cref="IdentityError"/> indicating a group is already in the specified <paramref name="resource"/>.
    /// </summary>
    /// <param name="group">The duplicate group.</param>
    /// <param name="resource">The duplicate resource.</param>
    /// <returns>An <see cref="IdentityError"/> indicating a group is already in the specified <paramref name="resource"/>.</returns>
    public static IdentityError GroupAlreadyInAccessRule(this IdentityErrorDescriber errorDescriber, string group, string resource)
    {
        return new IdentityError
        {
            Code = nameof(GroupAlreadyInAccessRule),
            Description = $"组织 {group} 对资源 {resource} 的访问规则已存在"
        };
    }

    /// <summary>
    /// Returns an <see cref="IdentityError"/> indicating a group is already in the specified <paramref name="resource"/>.
    /// </summary>
    /// <param name="level">The duplicate group.</param>
    /// <param name="resource">The duplicate resource.</param>
    /// <returns>An <see cref="IdentityError"/> indicating a group is already in the specified <paramref name="resource"/>.</returns>
    public static IdentityError LevelAlreadyInAccessRule(this IdentityErrorDescriber errorDescriber, string level, string resource)
    {
        return new IdentityError
        {
            Code = nameof(LevelAlreadyInAccessRule),
            Description = $"级别 {level} 对资源 {resource} 的访问规则已存在"
        };
    }

    /// <summary>
    /// Returns an <see cref="IdentityError"/> indicating a user is not in the specified <paramref name="resource"/>.
    /// </summary>
    /// <param name="user">The duplicate user.</param>
    /// <param name="resource">The duplicate resource.</param>
    /// <returns>An <see cref="IdentityError"/> indicating a user is not in the specified <paramref name="resource"/>.</returns>
    public static IdentityError UserNotInAccessRule(this IdentityErrorDescriber errorDescriber, string user, string resource)
    {
        return new IdentityError
        {
            Code = nameof(UserNotInAccessRule),
            Description = $"用户 {user} 对资源 {resource} 的访问规则不存在"
        };
    }

    /// <summary>
    /// Returns an <see cref="IdentityError"/> indicating a role is not in the specified <paramref name="resource"/>.
    /// </summary>
    /// <param name="role">The duplicate role.</param>
    /// <param name="resource">The duplicate resource.</param>
    /// <returns>An <see cref="IdentityError"/> indicating a role is not in the specified <paramref name="resource"/>.</returns>
    public static IdentityError RoleNotInAccessRule(this IdentityErrorDescriber errorDescriber, string role, string resource)
    {
        return new IdentityError
        {
            Code = nameof(RoleNotInAccessRule),
            Description = $"角色 {role} 对资源 {resource} 的访问规则已存在"
        };
    }

    /// <summary>
    /// Returns an <see cref="IdentityError"/> indicating a group is not in the specified <paramref name="resource"/>.
    /// </summary>
    /// <param name="group">The duplicate group.</param>
    /// <param name="resource">The duplicate resource.</param>
    /// <returns>An <see cref="IdentityError"/> indicating a group is not in the specified <paramref name="resource"/>.</returns>
    public static IdentityError GroupNotInAccessRule(this IdentityErrorDescriber errorDescriber, string group, string resource)
    {
        return new IdentityError
        {
            Code = nameof(GroupNotInAccessRule),
            Description = $"组织 {group} 对资源 {resource} 的访问规则已存在"
        };
    }

    /// <summary>
    /// Returns an <see cref="IdentityError"/> indicating a group is not in the specified <paramref name="resource"/>.
    /// </summary>
    /// <param name="level">The duplicate group.</param>
    /// <param name="resource">The duplicate resource.</param>
    /// <returns>An <see cref="IdentityError"/> indicating a group is not in the specified <paramref name="resource"/>.</returns>
    public static IdentityError LevelNotInAccessRule(this IdentityErrorDescriber errorDescriber, string level, string resource)
    {
        return new IdentityError
        {
            Code = nameof(GroupNotInAccessRule),
            Description = $"级别 {level} 对资源 {resource} 的访问规则已存在"
        };
    }
}